// pages/myidentityList/myidentityList.js
//logs.js
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var timeOut = 60;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    authCode: '',
    mobile: '',
    showModal: false,
    tipsCode: "获取验证码",
    timeNum: 60,
    clikType: false,
    currentTab: 0,
    role:[],
    roles:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var role = []
    // var list = {
    //   roleName: "身份介绍",
    //   elishRoleName: "介绍",
    //   icon: '',
    //   bgstart: '#ff9f3c',
    //   bgend: '#ff9f3c',
    //   bg: 1,
    // }

    // role.push(list);
    // var list = {
    //   roleName: "已备案",
    //   elishRoleName: "介绍 ",
    //   icon: '',
    //   bgstart: '#2666FC',
    //   bgend: '#54B0F1',
    //   bg: 2,
    // }
    // role.push(list);
    // var list = {
    //   roleName: "成为租户",
    //   elishRoleName: "介绍",
    //   icon: '',
    //   bgstart: '#2666FC',
    //   bgend: '#54B0F1',
    //   bg: 2,
    // }
    // role.push(list);
    // var list = {
    //   roleName: "成为员工",
    //   elishRoleName: "Owner",
    //   icon: '',
    //   bgstart: '#ff9f3c',
    //   bgend: '#ff9f3c',
    //   bg: 1,
    // }
    // role.push(list);

    // this.data.role = role;
    // this.(this.data);
   
    this.loadData();
    this.loadDataall();
  },
  chioce(e) {

    var index = e.currentTarget.dataset.index;

    var chiceid = e.currentTarget.dataset.id;

    this.setData({
      selected: index,
      chiceid: chiceid
    });

    switch (index) {
      case 0:

      
        break;
      case 1:
        this.openzp();
        break;
      case 2:
        wx.navigateTo({
          url: '../tenantCertification/tenantCertification',
        })
        break;
      case 3:
        wx.navigateTo({
          url: '../Employee_certification/Employee_certification',
        })
        break;
    }
  },
  go: function () {
    this.setData({
      showModal: false
    })
  },

  calsubmit() {
    var that = this
    let url = '';

    if (!that.data.calcontent) {
      util.showModal('请填写退回备注');
      return
    }
    var id = wx.getStorageSync("id");

    
    if (that.data.model == 'm1') {
      url = applet_park_api_url + '/api/repairs/return'

    }
    if (that.data.model == 'm2') {
      url = applet_park_api_url + '/api/repairs/cancel'

    }

    wx.request({
      url: url,
      data: {
        repairId: that.data.workid,
        assignorId: id,
        reason: that.data.calcontent
      },
      method: 'PUT',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      success: function (res) {
        
        if (res.data.code) {
          wx.showToast({
            title: '操作成功',
          })
          that.data.items = [];
          that.setData(that.data);
          that.setData({
            showModal: false
          })
          that.loadData();
        }
      },
      fail: function (e) {
        //util.showError("设置失败".e)
      }
    })
  },
  openzp: function (e) {
    // var workid = e.currentTarget.dataset.workid;
    // var model = e.currentTarget.dataset.model;
    this.setData({
      showModal: true,

    })
  },

  selectlogo: function (e) {
    var that = this;
    if (that.data.currentTab === e.currentTarget.dataset.index) {
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.index
      })
    }
  },
  bindKeyInput: function (e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);

  },
  // 点击获取手机验证码
  clickGainCode(e) {
    if (!this.data.clikType) {
      this.gainCode();
    }
  },
  //输入手机号获取验证码
  gainCode() {
    var that = this;
    if (this.data.mobile == '' || this.data.mobile == null) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
      })
    } else {
      that.setData({
        clikType: true
      })
      if (that.data.timeNum < timeOut) { } else {
        wx.request({
          url: applet_park_api_url + '/api/sendSms',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: "POST",
          data: {
            phoneNumber: this.data.mobile
          },
          success: function (res) {
            if (res.data.code == 0) {
              that.getTime();
              if (res.data.success) { } else { }
            } else {
              wx.showToast({
                title: '网络异常',
                icon: 'none',
              })
            }

          }
        })
      }
    }
  },
  //获取验证码时候显示时间
  getTime() {
    var that = this;
    var timer = setInterval(function () {
      that.data.timeNum--;
      if (that.data.timeNum <= 0) {
        that.setData({
          tipsCode: "重新获取验证码",
          timeNum: timeOut,
          clikType: false
        });
        clearInterval(timer);
      } else {
        that.setData({
          tipsCode: "发送" + that.data.timeNum + "秒",
        })
      }
    }, 1000)
  },

  loadData() {
    var that = this;
    var id = wx.getStorageSync("id");
    if(!id){
      return 
    }
    wx.request({
      url: applet_park_api_url + '/api/sysRoles',
      data:{
        sysUserId:id
      },
      success(res) {
        

        if (res.data.code == 0) {
          that.data.role = res.data.data
          that.setData(that.data);
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },

  loadDataall() {
    var that = this;
    var id = wx.getStorageSync("id");
    wx.request({
      url: applet_park_api_url + '/api/sysRoles',
      success(res) {
        

        if (res.data.code == 0) {
          that.data.roles = res.data.data
          that.setData(that.data);
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadData();
    this.loadDataall();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.loadData();
    wx.showToast({
      title: '刷新成功',
      icon: 'success',
      duration: 2000
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})